package com.fourthGroup.service.impl;

import com.fourthGroup.dao.OrderDao;
import com.fourthGroup.model.Order;
import com.fourthGroup.service.OrderService;
import com.fourthGroup.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    
    // 模拟订单数据存储
    private Map<String, Order> orderMap = new ConcurrentHashMap<>();
    // 模拟优惠券数据 - couponId -> 折扣金额或折扣率
    private Map<Long, Double> couponDiscountMap = new HashMap<>();
    
    public OrderServiceImpl() {
        // 初始化模拟的优惠券折扣数据
        couponDiscountMap.put(1L, 10.0); // 满减10元
        couponDiscountMap.put(2L, 0.8);  // 8折
    }

    @Override
    public void addOrder(Order order) {
        orderDao.addOrder(order);
    }

    @Override
    public Order queryOrderById(Long oid) {
        return orderDao.queryOrderById(oid);
    }

    @Override
    public Order addOrder(Integer uid, Integer pid) {
        return orderDao.addOrder(uid, pid);
    }

    @Override
    public void updateOrderStatus(Long oid) {
        orderDao.updateOrderStatus(oid);
    }
    
    @Override
    public Result<Order> createOrder(Order order) {
        // 生成订单ID
        if (order.getOid() == null) {
            order.setOid(System.currentTimeMillis());
        }
        
        // 设置初始状态
        order.setStatus(0);
        
        // 保存订单
        orderMap.put(String.valueOf(order.getOid()), order);
        log.info("创建订单成功: {}", order);
        return Result.success(order);
    }
    
    @Override
    public Result<Order> getOrder(Long oid) {
        Order order = orderMap.get(String.valueOf(oid));
        if (order == null) {
            order = orderDao.queryOrderById(oid);
        }
        
        if (order == null) {
            return Result.error("订单不存在");
        }
        return Result.success(order);
    }
    
    @Override
    public Result<Double> applyCoupon(String orderId, Long couponId) {
        Order order = orderMap.get(orderId);
        if (order == null) {
            return Result.error("订单不存在");
        }
        
        Double discount = couponDiscountMap.get(couponId);
        if (discount == null) {
            return Result.error("优惠券不存在");
        }
        
        double originalAmount = order.getPprice() * order.getNumber();
        double finalAmount;
        
        // 根据优惠券类型计算优惠后金额
        if (couponId == 1L) {
            // 满减券
            if (originalAmount >= 100) {
                finalAmount = originalAmount - discount;
            } else {
                return Result.error("订单金额不满足优惠券使用条件");
            }
        } else if (couponId == 2L) {
            // 折扣券
            finalAmount = originalAmount * discount;
        } else {
            return Result.error("不支持的优惠券类型");
        }
        
        log.info("订单{}应用优惠券{}后，金额从{}变为{}", orderId, couponId, originalAmount, finalAmount);
        return Result.success(finalAmount);
    }
}
